//----------------------------------------------------------------------------
// bmcmd.cpp
//
// bitmap stuff for nclip
//
// Copyright (C) 2011 Neil Butterworth
//----------------------------------------------------------------------------

#include <cstring>
#include <iostream>
#include <string>
#include "bmcmd.h"
#include "resource.h"
#include "util.h"

using namespace std;

//----------------------------------------------------------------------------
// Draw the bitmap in the dialog's client area
//----------------------------------------------------------------------------

static void DrawBitmap( HWND dlg ) {
	HDC dc = GetDC( dlg );
	HDC hdcMem = CreateCompatibleDC( dc );
	HBITMAP hbm = (HBITMAP) GetClipboardData( CF_BITMAP );
	SelectObject(hdcMem, hbm);
	RECT rc;
	GetClientRect( dlg, &rc);
	BitBlt( dc, 0, 0, rc.right, rc.bottom, hdcMem, 0, 0, SRCCOPY);
	DeleteDC( hdcMem );
	ReleaseDC( dlg, dc );
}

//----------------------------------------------------------------------------
// Windows proc for bitmap display dialog
//----------------------------------------------------------------------------

BOOL CALLBACK DialogProc( HWND hwnd, UINT message, WPARAM, LPARAM  ) {

    switch( message )    {

		case WM_PAINT:
			DrawBitmap( hwnd );
			return TRUE;

		case WM_COMMAND:
			return TRUE;

		case WM_INITDIALOG:
			return TRUE;

		case WM_DESTROY:
			PostQuitMessage(0);
			return TRUE;

		case WM_CLOSE:
			DestroyWindow (hwnd);
			return TRUE;
	}

    return FALSE;
}

//----------------------------------------------------------------------------
// Message loop runner for bitmap display dialog
//----------------------------------------------------------------------------

static int DoDialog()  {

	HWND dlg = CreateDialog( GetModuleHandle(0),
								MAKEINTRESOURCE( IDD_BITMAPDLG ),
								0, (DLGPROC) DialogProc );
	MSG  msg;

    while ( GetMessage( & msg, 0, 0, 0 ) ) {
        if ( ! IsDialogMessage( dlg, & msg ) ) {
            TranslateMessage( & msg );
            DispatchMessage( & msg );
        }
    }
	return 0;
}

//----------------------------------------------------------------------------
// Command to display bitmap, if there is one in clipboard
//----------------------------------------------------------------------------

int BGetCmd(  CmdLine & ) {
	Clipboard clp;
	if ( HasFormat( CF_BITMAP ) ) {
		DoDialog();
	}
	else {
		throw ClipError( "No bitmap data in clipboard" );
	}
	return 0;
}

//----------------------------------------------------------------------------
// Read bitmap from file and place in clipboard
//----------------------------------------------------------------------------

int BSetCmd(  CmdLine & cl ) {
	Clipboard clp;
	if ( cl.Argc() != 3 ) {
		throw ClipError( "Need single bitmap filename" );
	}
	string fname = cl.Argv( 2 );
	HBITMAP hb = (HBITMAP) LoadImage (0, fname.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	if ( hb == 0 ) {
		throw ClipError( "Could not read bitmap from " + fname );
	}
	EmptyClipboard();
	SetClipboardData( CF_BITMAP, hb );
	return 0;
}

// end
